using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Globalization;

namespace TST.SharePoint.Shared
{
    public class WSSFieldNumber : FieldNumber, IDataFormatting
    {
        private SPFieldNumber _field;

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldNumber()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">The ID for the field.</param>
        public WSSFieldNumber(string Id)
            : base(Id)
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldNumber(SPFieldNumber field)
        {
            this.Id = field.Id.ToString();
            _field = field;
        }

        public WSSFieldNumber(SPField field)
        {
            this.Id = field.Id.ToString();
            //_field = field;
        }

        public override string QueryOptions
        {
            get
            {
                if (_field != null && _field.ShowAsPercentage)
                    return " Percentage=\"TRUE\"";
                return string.Empty;
            }
        }

        #region IDataFormatting Members

        string IDataFormatting.DataFormatString
        {
            get
            {
                if (_field == null)
                    return string.Empty;

                return GetFormatString(_field.ShowAsPercentage, _field.DisplayFormat);
            }
        }

        #endregion

        public static string GetFormatString(bool showasPercentage, SPNumberFormatTypes format)
        {
            string formatLetter = "N";
            if (showasPercentage)
                formatLetter = "P";
            switch (format)
            {
                case SPNumberFormatTypes.FiveDecimals:
                    return "{0:" + formatLetter + "5}";
                case SPNumberFormatTypes.FourDecimals:
                    return "{0:" + formatLetter + "4}";
                case SPNumberFormatTypes.NoDecimal:
                    return "{0:" + formatLetter + "0}";
                case SPNumberFormatTypes.OneDecimal:
                    return "{0:" + formatLetter + "1}";
                case SPNumberFormatTypes.ThreeDecimals:
                    return "{0:" + formatLetter + "3}";
                case SPNumberFormatTypes.TwoDecimals:
                    return "{0:" + formatLetter + "2}";
                default:
                    if (formatLetter == "P")
                        return "{0:" + formatLetter + "0}";
                    else
                        return "{0:#}";
            }
        }

        public override object GetTypedValue(object value)
        {
            if (value == null)
                return null;
            string stringValue = value.ToString();
            double result;
            if (double.TryParse(stringValue.ToString(), System.Globalization.NumberStyles.Any, new CultureInfo(1033), out result))
                return result;
            return null;
        }

        public override string GetTypedValueAsString(object value)
        {
            if (value == null)
                return null;
            if (value is int)
            {
                return value.ToString(); // Counter
            }
            else
            {
                return ((double)value).ToString(new CultureInfo(1033));
            }
        }

        public override string GetTypedValueFormatted(object value)
        {

            if (value == null || _field == null)
                return null;
            IDataFormatting formatting = this as IDataFormatting;
            if (formatting != null)
            {
                return ((double)value).ToString(formatting.DataFormatString, new CultureInfo(1033));
            }
            return ((double)value).ToString(new CultureInfo(1033));
        }

    }
}
